package lims.client;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridBagLayout;

import javax.swing.BoxLayout;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;

import java.awt.GridBagConstraints;

import javax.swing.SwingConstants;
import javax.swing.JTextField;

import java.awt.Insets;
import java.util.Map;

import javax.swing.JPasswordField;

import lims.bridge.remoteprint.Main;

public class ConfigDialog extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5003653080196577008L;

	public boolean modalResult=false;

	private final JPanel contentPanel = new JPanel();
	private JTextField serverURL;
	private JTextField uid;
	private JPasswordField pwd;

	private JTextField authUserName;
	private JPasswordField authPassword;
	
	private JTextField bridgeId;
	private JTextField bridgeAddress;
	private JTextField bridgeUserName;
	private JPasswordField bridgePassword;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			ConfigDialog dialog = new ConfigDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public ConfigDialog() {
		setTitle("参数设置");
		setBounds(100, 100, 441, 215);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.X_AXIS));
		{
			JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
			contentPanel.add(tabbedPane);
			{
				JPanel panel = new JPanel();
				tabbedPane.addTab("数据服务器", null, panel, null);
				GridBagLayout gbl_panel = new GridBagLayout();
				gbl_panel.rowHeights = new int[]{0, 0, 0, 0};
				gbl_panel.columnWeights = new double[]{0.0, 1.0, 0.0, 1.0};
				gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
				panel.setLayout(gbl_panel);
				{
					JLabel lblNewLabel = new JLabel("服务器地址：");
					lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
					GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
					gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
					gbc_lblNewLabel.anchor = GridBagConstraints.EAST;
					gbc_lblNewLabel.gridx = 0;
					gbc_lblNewLabel.gridy = 0;
					panel.add(lblNewLabel, gbc_lblNewLabel);
				}
				{
					serverURL = new JTextField();
					GridBagConstraints gbc_serverURL = new GridBagConstraints();
					gbc_serverURL.gridwidth = 3;
					gbc_serverURL.insets = new Insets(0, 0, 5, 5);
					gbc_serverURL.fill = GridBagConstraints.HORIZONTAL;
					gbc_serverURL.gridx = 1;
					gbc_serverURL.gridy = 0;
					panel.add(serverURL, gbc_serverURL);
				}
				{
					JLabel lblNewLabel_1 = new JLabel("外层授权用户：");
					GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
					gbc_lblNewLabel_1.anchor = GridBagConstraints.EAST;
					gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
					gbc_lblNewLabel_1.gridx = 0;
					gbc_lblNewLabel_1.gridy = 1;
					panel.add(lblNewLabel_1, gbc_lblNewLabel_1);
				}
				{
					authUserName = new JTextField();
					GridBagConstraints gbc_authUserName = new GridBagConstraints();
					gbc_authUserName.insets = new Insets(0, 0, 5, 5);
					gbc_authUserName.fill = GridBagConstraints.HORIZONTAL;
					gbc_authUserName.gridx = 1;
					gbc_authUserName.gridy = 1;
					panel.add(authUserName, gbc_authUserName);
				}
				{
					JLabel lblNewLabel_2 = new JLabel("外层授权密码：");
					GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
					gbc_lblNewLabel_2.anchor = GridBagConstraints.EAST;
					gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5);
					gbc_lblNewLabel_2.gridx = 2;
					gbc_lblNewLabel_2.gridy = 1;
					panel.add(lblNewLabel_2, gbc_lblNewLabel_2);
				}
				{
					authPassword = new JPasswordField();
					GridBagConstraints gbc_authPassword = new GridBagConstraints();
					gbc_authPassword.insets = new Insets(0, 0, 5, 5);
					gbc_authPassword.fill = GridBagConstraints.HORIZONTAL;
					gbc_authPassword.gridx = 3;
					gbc_authPassword.gridy = 1;
					panel.add(authPassword, gbc_authPassword);
				}
				{
					JLabel label = new JLabel("登录用户：");
					GridBagConstraints gbc_label = new GridBagConstraints();
					gbc_label.anchor = GridBagConstraints.EAST;
					gbc_label.insets = new Insets(0, 0, 0, 5);
					gbc_label.gridx = 0;
					gbc_label.gridy = 2;
					panel.add(label, gbc_label);
				}
				{
					uid = new JTextField();
					GridBagConstraints gbc_uid = new GridBagConstraints();
					gbc_uid.insets = new Insets(0, 0, 0, 5);
					gbc_uid.fill = GridBagConstraints.HORIZONTAL;
					gbc_uid.gridx = 1;
					gbc_uid.gridy = 2;
					panel.add(uid, gbc_uid);
				}
				{
					JLabel label = new JLabel("登录密码：");
					GridBagConstraints gbc_label = new GridBagConstraints();
					gbc_label.insets = new Insets(0, 0, 0, 5);
					gbc_label.anchor = GridBagConstraints.EAST;
					gbc_label.gridx = 2;
					gbc_label.gridy = 2;
					panel.add(label, gbc_label);
				}
				{
					pwd = new JPasswordField();
					GridBagConstraints gbc_pwd = new GridBagConstraints();
					gbc_pwd.insets = new Insets(0, 0, 0, 5);
					gbc_pwd.fill = GridBagConstraints.HORIZONTAL;
					gbc_pwd.gridx = 3;
					gbc_pwd.gridy = 2;
					panel.add(pwd, gbc_pwd);
				}
			}
			{
				JPanel panel = new JPanel();
				tabbedPane.addTab("通讯设置", null, panel, null);
				GridBagLayout gbl_panel = new GridBagLayout();
				gbl_panel.rowHeights = new int[]{0, 0, 0, 0};
				gbl_panel.columnWeights = new double[]{0.0, 1.0, 0.0, 1.0};
				gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
				panel.setLayout(gbl_panel);
				{
					JLabel label = new JLabel("本节点名称：");
					GridBagConstraints gbc_label = new GridBagConstraints();
					gbc_label.insets = new Insets(0, 0, 5, 5);
					gbc_label.anchor = GridBagConstraints.EAST;
					gbc_label.gridx = 0;
					gbc_label.gridy = 0;
					panel.add(label, gbc_label);
				}
				{
					bridgeId = new JTextField();
					GridBagConstraints gbc_bridgeId = new GridBagConstraints();
					gbc_bridgeId.insets = new Insets(0, 0, 5, 5);
					gbc_bridgeId.fill = GridBagConstraints.HORIZONTAL;
					gbc_bridgeId.gridx = 1;
					gbc_bridgeId.gridy = 0;
					panel.add(bridgeId, gbc_bridgeId);
				}
				{
					JLabel label = new JLabel("服务器地址：");
					GridBagConstraints gbc_label = new GridBagConstraints();
					gbc_label.anchor = GridBagConstraints.EAST;
					gbc_label.insets = new Insets(0, 0, 5, 5);
					gbc_label.gridx = 0;
					gbc_label.gridy = 1;
					panel.add(label, gbc_label);
				}
				{
					bridgeAddress = new JTextField();
					GridBagConstraints gbc_jmsBrokerURL = new GridBagConstraints();
					gbc_jmsBrokerURL.gridwidth = 3;
					gbc_jmsBrokerURL.insets = new Insets(0, 0, 5, 5);
					gbc_jmsBrokerURL.fill = GridBagConstraints.HORIZONTAL;
					gbc_jmsBrokerURL.gridx = 1;
					gbc_jmsBrokerURL.gridy = 1;
					panel.add(bridgeAddress, gbc_jmsBrokerURL);
					bridgeAddress.setColumns(10);
				}
				{
					JLabel label = new JLabel("用户名：");
					GridBagConstraints gbc_label = new GridBagConstraints();
					gbc_label.anchor = GridBagConstraints.EAST;
					gbc_label.insets = new Insets(0, 0, 0, 5);
					gbc_label.gridx = 0;
					gbc_label.gridy = 2;
					panel.add(label, gbc_label);
				}
				{
					bridgeUserName = new JTextField();
					GridBagConstraints gbc_jmsUserName = new GridBagConstraints();
					gbc_jmsUserName.insets = new Insets(0, 0, 0, 5);
					gbc_jmsUserName.fill = GridBagConstraints.HORIZONTAL;
					gbc_jmsUserName.gridx = 1;
					gbc_jmsUserName.gridy = 2;
					panel.add(bridgeUserName, gbc_jmsUserName);
				}
				{
					JLabel lblNewLabel_3 = new JLabel("密码：");
					GridBagConstraints gbc_lblNewLabel_3 = new GridBagConstraints();
					gbc_lblNewLabel_3.insets = new Insets(0, 0, 0, 5);
					gbc_lblNewLabel_3.anchor = GridBagConstraints.EAST;
					gbc_lblNewLabel_3.gridx = 2;
					gbc_lblNewLabel_3.gridy = 2;
					panel.add(lblNewLabel_3, gbc_lblNewLabel_3);
				}
				{
					bridgePassword = new JPasswordField();
					GridBagConstraints gbc_jmsPassword = new GridBagConstraints();
					gbc_jmsPassword.insets = new Insets(0, 0, 0, 5);
					gbc_jmsPassword.fill = GridBagConstraints.HORIZONTAL;
					gbc_jmsPassword.gridx = 3;
					gbc_jmsPassword.gridy = 2;
					panel.add(bridgePassword, gbc_jmsPassword);
				}
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("确定");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						modalResult=true;
						dispose();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("取消");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						modalResult=false;
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	public void loadConfig() {
		Map<String,Object> userVariables=Main.userVariables;
		serverURL.setText((String) userVariables.get("data_url"));
		uid.setText((String) userVariables.get("data_uid"));		
		if(userVariables.get("data_pwd")!=null)
		pwd.setText(new String((char[]) userVariables.get("data_pwd")));
		
		authUserName.setText((String) userVariables.get("auth_uid"));		
		if(userVariables.get("auth_pwd")!=null)
		authPassword.setText(new String((char[]) userVariables.get("auth_pwd")));
		
		bridgeId.setText((String) userVariables.get("bridge_id"));		
		bridgeAddress.setText((String) userVariables.get("bridge_address"));
		bridgeUserName.setText((String) userVariables.get("bridge_username"));
		if(userVariables.get("bridge_password")!=null)
		bridgePassword.setText(new String((char[]) userVariables.get("bridge_password")));
		
	}

	public void updateConfig() {
		Map<String,Object> userVariables=Main.userVariables;
		
		
		userVariables.put("data_url",serverURL.getText());
		userVariables.put("data_uid",uid.getText());
		userVariables.put("data_pwd",pwd.getPassword());
		userVariables.put("auth_uid",authUserName.getText());
		userVariables.put("auth_pwd",authPassword.getPassword());

		userVariables.put("bridge_id",bridgeId.getText());
		userVariables.put("bridge_address",bridgeAddress.getText());
		userVariables.put("bridge_username",bridgeUserName.getText());
		userVariables.put("bridge_password",bridgePassword.getPassword());
		
		

		
	}
}
